package Array;

import org.junit.Test;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Array
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-19  22:19
 * @Description: TODO
 * @Version: 1.0
 */
public class 最后一个单词的长度58 {

    public int lengthOfLastWord(String s) {
        String[] s1 = s.trim().split(" ");
        return s1[s1.length-1].length();
    }
    // 利用反向遍历的方式来实现
    public int lengthOfLastWord2(String s) {
        int index = s.length() - 1;
        while (s.charAt(index) == ' ') {
            index--;
        }
        int wordLength = 0;
        while (index >= 0 && s.charAt(index) != ' ') {
            wordLength++;
            index--;
        }
        return wordLength;
    }

    @Test
    public void test(){
        int i = lengthOfLastWord("luffy is still joyboy");
        System.out.println(i);
    }
}
